/******************************************************************************
 * @File name   :      CardDefinitionDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Aug 11, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Aug 11, 2015 6:02:26 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.coupon.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.coupon.dao.ICouponBatchDao;
import com.cap.bts.crmdp.dealer.coupon.domain.CouponBatch;
import com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Pager;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.common.util.WhereConditionGenerator;

/**
 *优惠券活动批次
 */
@Repository("couponBatchDao")
public class CouponBatchDaoImpl extends BaseDaoImpl<CouponBatch, Long> implements ICouponBatchDao {

    /**
     * @Date        :      Aug 11, 2015
     * @param clazz
     */
    @Inject
    public CouponBatchDaoImpl(@Value("com.cap.bts.crmdp.dealer.coupon.domain.CouponBatch") Class<CouponBatch> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 8, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponBatchDao#getCouponBatchAllList(com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto)
    **/
    @Override
    public List<CouponBatch> getCouponBatchAllList(CouponBatchDto dto) throws AppException {
        String hql = "select a from CouponBatch a";
        String countHql = "select count(*) ";

        String fromStr = "from CouponBatch a";
        String whereStr = " where 1=1";

        WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);

        String orderHql = " order by a.id desc";

        Pager page = dto.getPager();

        page.setPageSize(10);

        return getListByQueryWithPaging(hql + condition.toString() + orderHql,
            countHql + fromStr + condition.toString(), dto.getPager());
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年9月16日
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponBatchDao#getCouponBatchListwithQuery(com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto)
    **/
    @Override
    public List<CouponBatch> getCouponBatchListwithQuery(CouponBatchDto dto) throws AppException {
        String hql = "select a from CouponBatch a";
        String countHql = "select count(*) ";

        String fromStr = "from CouponBatch a";
        String whereStr = " where 1=1";
        
        String preRequests="";
        if(Utils.notEmpty(dto.getTransactionStartDate())){
            preRequests+= " and TO_DAYS(a.startDate) >= TO_DAYS('" + dto.getTransactionStartDate() + "')";
        }
        if(Utils.notEmpty(dto.getTransactionEndDate())){
            preRequests+= " and TO_DAYS(a.startDate) <= TO_DAYS('" + dto.getTransactionEndDate() + "')";
        }
        
        String couponName = "";
        if(Utils.notEmpty(dto.getBatchName())){
            couponName+= " and a.batchName like '%"+dto.getBatchName()+"%'";
        }

        WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);

        String orderHql = " order by a.id desc";

        Pager page = dto.getPager();

        page.setPageSize(10);

        return getListByQueryWithPaging(hql + condition.toString() + preRequests + couponName + orderHql,
            countHql + fromStr + condition.toString(), dto.getPager());
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年9月16日
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponBatchDao#getExportCouponBatchListwithQuery(com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto)
    **/
    @Override
    public List<CouponBatch> getExportCouponBatchListwithQuery(CouponBatchDto dto) throws AppException {
        String hql = "select a from CouponBatch a";
        String countHql = "select count(*) ";

        String fromStr = "from CouponBatch a";
        String whereStr = " where a.isGenerated=1";
        
        String preRequests="";
        if(Utils.notEmpty(dto.getTransactionStartDate())){
            preRequests+= " and TO_DAYS(a.startDate) >= TO_DAYS('" + dto.getTransactionStartDate() + "')";
        }
        if(Utils.notEmpty(dto.getTransactionEndDate())){
            preRequests+= " and TO_DAYS(a.startDate) <= TO_DAYS('" + dto.getTransactionEndDate() + "')";
        }
        
        String couponName = "";
        if(Utils.notEmpty(dto.getBatchName())){
            couponName+= " and a.batchName like '%"+dto.getBatchName()+"%'";
        }

        WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);

        String orderHql = " order by a.id desc";

        Pager page = dto.getPager();

        page.setPageSize(10);

        return getListByQueryWithPaging(hql + condition.toString() + preRequests + couponName + orderHql,
            countHql + fromStr + condition.toString(), dto.getPager());
    }

    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 27, 2015
     * @see com.cap.bts.crmdp.dealer.coupon.dao.ICouponBatchDao#getExportCouponBatchAllList(com.cap.bts.crmdp.dealer.coupon.dto.CouponBatchDto)
    **/
    @Override
    public List<CouponBatch> getExportCouponBatchAllList(CouponBatchDto dto) throws AppException {
        String hql = "select a from CouponBatch a";
        String countHql = "select count(*) ";

        String fromStr = "from CouponBatch a";
        String whereStr = " where a.isGenerated=1";

        WhereConditionGenerator condition = new WhereConditionGenerator(whereStr);

        String orderHql = " order by a.id desc";

        Pager page = dto.getPager();

        page.setPageSize(10);

        return getListByQueryWithPaging(hql + condition.toString() + orderHql,
            countHql + fromStr + condition.toString(), dto.getPager());
    }
}
